<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Greeting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GreetingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Greeting(['category']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('category.title','分类');
            $grid->column('content');
            $grid->column('copy_count');
            $grid->column('sort','排序');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->quickSearch(['category.title','content']);
            $grid->disableViewButton();

            $grid->model()->orderBy('sort', 'asc');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Greeting(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('content');
            $show->field('copy_count');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Greeting(), function (Form $form) {
            $form->display('id');
            $form->select('category_id')->options('apiList');
            $form->textarea('content');
            $form->number('sort','排序')->default(99)->required()->min(0);
            $form->hidden('copy_count')->default(0);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
